package com.changli03.threadsafe.runnablesafe;

public class noSafeTest1 {
    public static void main(String[] args) {
        TicketThread1 tt1 = new TicketThread1();
        //共用一个类
        Thread t1 = new Thread(tt1);
        Thread t2 = new Thread(tt1);
        Thread t3 = new Thread(tt1);

        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");

        t1.start();
        t2.start();
        t3.start();

    }
}
class TicketThread1 implements Runnable {
    int ticketnum = 100 ;
    Object lock = new Object();
    boolean flag = true;
    @Override
    public void run() {

        while (flag){
            show();


        }
    }
    public synchronized void show (){ // 默认的lock 为 this 且无法设置
        if (ticketnum > 0){
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName() + "票号为:" + ticketnum);
            ticketnum--;
        }else {
            flag = false;
        }
    }
}
